﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using ApartmentBillsManager.Start.Annotations;

namespace ApartmentBillsManager.Start.Common
{
    public class Bill : INotifyPropertyChanged
    {
        private readonly Guid _guid;
        private DateTime _startDate;
        private DateTime _endDate;
        private DateTime _dateClosed;
        private List<BillPhase> _phases;
        private Roommate _paid;

        private Bill() {}

        public Bill(BillCategory category, decimal amount, DateTime startDate, DateTime endDate, Roommate paid, DateTime datePaid, string remark)
        {
            Category = category;
            Amount = amount;
            StartDate = startDate;
            EndDate = endDate;
            Paid = paid;
            DatePaid = datePaid;
            _guid = Guid.NewGuid();
            Remark = remark;
        }

        public BillCategory Category { get; set; }
        public decimal Amount { get; set; }

        public DateTime StartDate
        {
            get { return _startDate.Date; }
            set
            {
                _startDate = value;
                OnPropertyChanged("TotalDays");
            }
        }

        public DateTime EndDate
        {
            get { return _endDate.Date; }
            set
            {
                _endDate = value;
                OnPropertyChanged("TotalDays");
            }
        }

        public List<BillPhase> Phases
        {
            get { return _phases; }
            set
            {
                _phases = value;
                OnPropertyChanged("Phases");
            }
        }

        public int TotalDays
        {
            get { return EndDate.Subtract(StartDate).Days + 1; }
        }

        public Roommate Paid
        {
            get { return _paid; }
            set
            {
                _paid = value;
                OnPropertyChanged("Paid");
            }
        }

        public DateTime DatePaid { get; set; }

        public DateTime DateClosed
        {
            get { return _dateClosed.Date; }
            set { _dateClosed = value; }
        }

        public Guid Guid
        {
            get { return _guid; }
        }

        public string Remark { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}
